package com.tust.androidreader;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {
    private Toast toast;
    private int isEye = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();//隐藏标题导航栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//去除时间和电量等状态栏
        setContentView(R.layout.activity_login);

        EditText editTextUserName = this.findViewById(R.id.et_username);
        EditText editTextPassword = this.findViewById(R.id.et_password);
        ImageView imageViewEye = this.findViewById(R.id.iv_eye);
        Button buttonLogin = this.findViewById(R.id.bt_login);
        TextView textViewGoToRegister = this.findViewById(R.id.tv_gotoregister);

        buttonLogin.setBackgroundColor(Color.rgb(153, 217, 234));
        textViewGoToRegister.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);

        editTextUserName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {//获得焦点
                    if (editTextUserName.getText().toString().equals("输入用户名")) {
                        editTextPassword.setTextColor(Color.rgb(128,128,128));
                        editTextUserName.setText("");
                    }
                } else {
                    if (editTextUserName.getText().toString().equals("")) {
                        editTextPassword.setTextColor(Color.rgb(159,159,159));
                        editTextUserName.setText("输入用户名");
                    }
                }
            }
        });

        editTextPassword.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {//获得焦点
                    if (editTextPassword.getText().toString().equals("输入密码")) {
                        editTextPassword.setTextColor(Color.rgb(128,128,128));
                        editTextPassword.setText("");
                        editTextPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);//密码不可见
                        imageViewEye.setVisibility(View.VISIBLE);
                        imageViewEye.setImageResource(R.drawable.baseline_visibility_off_black_18dp);
                        isEye = 0;
                    }
                } else {
                    if (editTextPassword.getText().toString().equals("")) {
                        editTextPassword.setInputType(InputType.TYPE_CLASS_TEXT);
                        editTextPassword.setTextColor(Color.rgb(159,159,159));
                        editTextPassword.setText("输入密码");
                        imageViewEye.setVisibility(View.INVISIBLE);//不显示img
                        isEye = -1;
                    }
                }
            }
        });

        imageViewEye.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isEye == 0) {
                    editTextPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);//密码可见
                    imageViewEye.setImageResource(R.drawable.baseline_remove_red_eye_black_18dp);
                    isEye = 1;
                } else if (isEye == 1) {
                    editTextPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);//密码不可见
                    imageViewEye.setImageResource(R.drawable.baseline_visibility_off_black_18dp);
                    isEye = 0;
                }
            }
        });

        DBHelper dbHelper = new DBHelper(this);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        buttonLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String userName = editTextUserName.getText().toString();
                String password = editTextPassword.getText().toString();

                if (userName.equals("")) {
                    showToast("用户名不能为空！");
                } else if (password.equals("")) {
                    showToast("密码不能为空！");
                } else  if (userName.equals("输入用户名")) {
                    showToast("请输入用户名！");
                } else if(password.equals("输入密码")) {
                    showToast("请输入密码！");
                } else {
                    Cursor userCursor = db.rawQuery("select * from user where username = ?", new String[]{userName});
                    try {
                        if (userCursor.getCount() < 1) {//没有找到该用户名，这里游标会自动下移
                            showToast("不存在的用户名！");
                        } else if (userCursor.moveToFirst()) {
                            String dbUsername = userCursor.getString(0);
                            String dbPassword = userCursor.getString(1);

                            if (userName.equals(dbUsername) && password.equals(dbPassword)) {
                                showToast("登陆成功！");
                                Intent intent = new Intent(LoginActivity.this, LibraryActivity.class);
                                intent.putExtra("username", userName);
                                startActivity(intent);
                            } else {
                                showToast("密码错误！");
                            }
                        } else {
                            showToast("数据库读取失败！");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    userCursor.close();
                }
            }
        });

        textViewGoToRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onBackPressed() {
        //super.onBackPressed();
    }

    private void showToast(String s) {
        if (toast == null){
            toast = Toast.makeText(this, s, Toast.LENGTH_SHORT);
        }else {
            toast.cancel();
            toast = Toast.makeText(this, s, Toast.LENGTH_SHORT);
        }
        toast.show();
    }
}